/*
 * Dimension.h
 *
 *  Created on: Sep 4, 2011
 *      Author: topcoder
 */

#ifndef DIMENSION_H_
#define DIMENSION_H_
#include <iostream>
#include <string>
using namespace std;

template <class T1>
class Dimension
{
	typedef Dimension<T1> self;
public:
	Dimension(int amount,T1& unit);
	//Dimension(self &a);

	bool operator==(const self &a) const;
	bool operator!=(const self  &a) const;
	self operator+(const self &a) const;
	string toString();
    string toStringInBaseUnit();

private:
	int amount;
	T1& unit;
};

template<class T1>
Dimension<T1>::Dimension(int amount,T1& unit)
:amount(amount),
unit(unit)
{

}

template<class T1>
bool Dimension<T1>::operator==(Dimension<T1> const &a) const
{
	return (amount*unit.getFactor() == a.amount*a.unit.getFactor());
}

template<class T1>
bool Dimension<T1>::operator!=(Dimension<T1> const &a) const
{
	return !(*this == a);
}

template<class T1>
Dimension<T1> Dimension<T1>::operator+(Dimension<T1> const &a) const
{

	 int tempAmount = amount*unit.getFactor() + a.amount*a.unit.getFactor();
	 Dimension<T1> temp(tempAmount/unit.getFactor(),unit);

	 return temp;
}

template<class T1>
string Dimension<T1>::toString()
{
	 return unit.formatToString(unit.getFactor()*amount);
}

template<class T1>
string Dimension<T1>::toStringInBaseUnit()
{
	 return unit.formatToStringInBaseUnit(unit.getFactor()*amount);
}

#endif /* DIMENSION_H_ */ 
